using KinonekoSoftware.CodariaDB.Documents.Index;

namespace KinonekoSoftware.CodariaDB.Documents.Templates
{
    partial class TemplateEngine
    {
        private void InitializeInstanceTemplates()
        {
            //
            // 
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Ability));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Character));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Species));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Plant));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Team));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Geography));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.PoliticalGroups_Country));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Item_Equipment));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Item_Weapon));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Item_Material));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Item_Item));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Item_Resources));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Others_School));
            InstanceTemplateDB.Insert(InstanceTemplateGenerator.Generate(InstanceType.Others_Class));
        }


        public InstanceTemplate HasInstanceTemplate(InstanceType type) => InstanceTemplateDB.FindOne(x => x.BuiltinType == type && !x.UseCustomType);

        public void Add(InstanceTemplate target) => InstanceTemplateDB.Insert(target);

        public void UpdateInstanceTemplate(InstanceTemplate target) => InstanceTemplateDB.Update(target);


        public void Remove(InstanceTemplate target) => InstanceTemplateDB.Delete(target.Id);
        
        public IEnumerable<InstanceTemplate> GetInstanceTemplates() => InstanceTemplateDB.FindAll();
        
        public InstanceTemplate GetInstanceTemplate(InstanceCache cache)
        {
            Debug.Assert(cache is not null);

            if (cache.UseCustomType)
            {
                return InstanceTemplateDB.FindOne(x => x.UseCustomType && x.CustomType == cache.CustomType);
            }
            
            return InstanceTemplateDB.FindOne(x => !x.UseCustomType && x.BuiltinType == cache.Type);
        }
        
        public InstanceTemplate GetInstanceTemplate(InstanceType type)
        {
            return InstanceTemplateDB.FindOne(x => !x.UseCustomType && x.BuiltinType == type);
        }

        public void ReGenerate()
        {
            //
            //
            InstanceTemplateDB.DeleteAll();
            
            //
            //
            InitializeInstanceTemplates();
            
            //
            //
            Modified();
        }
    }
}